package zd.cornermemory.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import zd.cornermemory.R;
import zd.cornermemory.db.RememberCj;
import zd.cornermemory.utils.Statistics;

import static zd.cornermemory.R.id.code;

public class RememberCjAdapter extends BaseAdapter {
	private Context        mContext;
	private List<RememberCj>       cjs;
	private LayoutInflater mInflater;

	final class ViewHolder {
		public TextView cj,code;
	}

	public RememberCjAdapter(Context context, List<RememberCj> cjs) {
		this.mContext = context;
		this.mInflater = LayoutInflater.from(context);
		this.cjs = cjs;
	}

	@Override
	public int getCount() {
		return cjs==null?0:cjs.size();
	}

	@Override
	public Object getItem(int arg0) {

		return cjs==null?null:cjs.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			int resId = R.layout.remember_cj_item;
			convertView = mInflater.inflate(resId, null);
			holder.cj = (TextView) convertView.findViewById(R.id.sj);
			holder.code = (TextView) convertView.findViewById(code);
			convertView.setTag(holder);
		} else{
			holder = (ViewHolder) convertView.getTag();
		}

		RememberCj cj = cjs.get(position);
		String date = Statistics.handleLongDate(cj.getDate());
		String code = cj.getCode();
		code = showCode(Pro(code));

		String time = Statistics.timeToString(cj.getSingTime()+cj.getErrorTime());
		switch (cj.getStatus()) {
			case 0:
				int timeTotal = cj.getSingTime();
				int rememberTime = cj.getRememberTime();
				int opreTime = timeTotal-rememberTime;
				String timeTotalStr = Statistics.timeToString(timeTotal);
				String rememberTimeStr = Statistics.timeToString(rememberTime);
				String opreTimeStr = Statistics.timeToString(opreTime);
				time = timeTotalStr+"  ( "+rememberTimeStr+" + "+opreTimeStr+" )";
				break;
			case 1://+2
				time = Statistics.timeToString(cj.getSingTime());
				time += "+";
				break;
			case 2:
				time = "DNF";
				break;

		}

		holder.cj.setText(time);
		int type = cj.getType();
		holder.code.setText("["+date+"]   "+code);
		if (type == 2) {
			String edgeCode = mContext.getResources().getString(R.string.edge_code);
			String cornerCode = mContext.getResources().getString(R.string.corner_code);
			String re = edgeCode+showCode(Pro(cj.getEdgeCode()))+cornerCode+showCode(Pro(cj.getCode()));
			holder.code.setText("["+date+"]   "+re);
		}

		return convertView;
	}

	private String showCode(String[] str){
		String result = "";
		for(int i=0;i<str.length;i++){
			result += str[i];
			result += " ";
		}
		return result;
	}

	private String[] Pro(String number){
		String[] str;
		int length = number.length();
		int group = length/2;
		if(0==length % 2)
		{
			str=new String[group];
		}
		else
		{
			str= new String[group+1];
		}
		for(int i=0,j=0;i<group;i++,j+=2)
		{
			str[i]=number.substring(j, j+2);
			if(i==(group-1))
			{
				if(1 == length % 2)
				{
					str[i+1] = number.substring(length-1,length);
				}
			}
		}
		return str;
	}
}
